package com.neu.view.impl;

import com.neu.app.App;
import com.neu.dao.BussinessDao;
import com.neu.dao.FoodDao;
import com.neu.dao.impl.BussinessDaoImpl;
import com.neu.dao.impl.FoodDaoImpl;
import com.neu.util.Read;
import com.neu.view.UserView;

import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class BussinessView extends UserView {
    private BussinessDao bussinessDao = new BussinessDaoImpl();
    private FoodDao foodDao = new FoodDaoImpl();

    @Override
    //用户登录界面
    public Map<String, Object> login() {
        Map<String, Object> map = null;
        Scanner in = new Scanner(System.in);
        System.out.println("商家账号：");
        String name = in.nextLine();
        System.out.println("商家密码：");
        String pwd = in.nextLine();
        map = bussinessDao.login(name, pwd);
        return map;
    }

    @Override
    public void returnLevel() {
        System.out.println("1.返回首页");
        System.out.println("2.返回登录页");
        System.out.println("3.退出系统");
        int n = Read.getIn().nextInt();
        switch (n) {
            case 1:
                menu();
                break;
            case 2:
                App.main();
                break;
            case 3:
                System.exit(0);
                break;
            default:
                System.out.println("请选择功能！");
                break;
        }
    }

    @Override
    //主菜单，商家选择功能
    public void menu() {
        System.out.println("商家管理后台：");
        System.out.println("1.查询食品");
        System.out.println("2.添加食品");
        System.out.println("3.删除食品");
        System.out.println("4.修改食品");
        System.out.println("5.修改密码");
        System.out.println("6.修改信息");
        System.out.println("7.退出系统");
        int n = Read.getIn().nextInt();
        switch (n) {
            case 1:
                searchFoodMenu();
                break;
            case 2:
                addFood();
                break;
            case 3:
                deleteFood();
                break;
            case 4:
                editFood();
                break;
            case 5:
                editPwd();
                break;
            case 6:
                editBussiness();
                break;
            case 7:
                System.exit(0);
                break;
            default:
                System.out.println("请选择正确的功能！");
                returnLevel();
        }

    }

    private void editFood() {
        //编辑食品信息
        System.out.println("请输入要修改的商品编号：");
        String foodId = Read.getIn().next();
        //获取当前商家编号
        String id = getInfo().get("bussiness_id").toString();
        Map<String,Object> map= foodDao.findById(foodId,id);
        if(map==null){
            System.out.println("您输入的商品编号有误！");
            returnLevel();
        }
        System.out.println("商品编号\t商品名称\t商品价格\t商品介绍");
        Object name = map.get("food_name");
        Object price = map.get("food_price");
        Object explain = map.get("food_explain");
        Object status = map.get("food_status");
        System.out.println(foodId+"\t"+name+"\t"+price+"\t"+explain+"\t"+status);
        System.out.println("是否修改？1.是||2.否");
        int n = Read.getIn().nextInt();
        if(n!=1){
            returnLevel();
        }
        System.out.println("请输入名称：");
        String foodName = Read.getIn().next();
        System.out.println("请输入价格：");
        String foodPrice = Read.getIn().next();
        System.out.println("请输入介绍：");
        String foodExplain = Read.getIn().next();
        System.out.println("请输入状态：");
        String foodStatus = Read.getIn().next();
        int r = foodDao.editFood(id,foodName,foodPrice,foodExplain,foodStatus);
        if (r>0){
            System.out.println("修改成功！");
        }else {
            System.out.println("修改失败！");
        }
        returnLevel();


    }

    private void deleteFood() {
        //删除食品
        System.out.println("请输入要删除的商品编号：");
        String foodId = Read.getIn().next();
        //获取当前商家编号
        String id = getInfo().get("bussiness_id").toString();
        Map<String,Object> map= foodDao.findById(foodId,id);
        if(map==null){
            System.out.println("您输入的商品编号有误！");
            returnLevel();
        }
        System.out.println("商品编号\t商品名称\t商品价格\t商品介绍\t商品状态");
        Object name = map.get("food_name");
        Object price = map.get("food_price");
        Object explain = map.get("food_explain");
        Object status = map.get("food_status");
        System.out.println(foodId+"\t"+name+"\t"+price+"\t"+explain+"\t"+status);
        System.out.println("是否删除？1.是||2.否");
        int n = Read.getIn().nextInt();
        if(n!=1){
            returnLevel();
        }
        int r = foodDao.deleteFood(foodId);
        if (r>0){
            System.out.println("删除成功！");
        }else {
            System.out.println("删除失败！");
        }
        returnLevel();

    }

    private void editBussiness() {
        //修改商家信息
        Object name = getInfo().get("bussiness_name");
        System.out.println("商家名称为：" + name + "不允许修改！");
        String id = getInfo().get("bussiness_id").toString();
        System.out.println("商家地址：");
        String addr = Read.getIn().next();
        System.out.println("商家简介：");
        String explain = Read.getIn().next();
        System.out.println("起送价：");
        String startPrice = Read.getIn().next();
        System.out.println("配送费：");
        String deliveryPrice = Read.getIn().next();
        int r = bussinessDao.updateBussiness(addr, explain, startPrice, deliveryPrice, id);
        if (r > 0) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
        returnLevel();
    }

    private void editPwd() {
        //修改密码
        System.out.println("请输入旧密码：");
        String oldPwd = Read.getIn().next();
        System.out.println("请输入新密码：");
        String newPwd = Read.getIn().next();
        System.out.println("请确认密码：");
        String rePwd = Read.getIn().next();
        if (oldPwd.equals(newPwd)) {
            System.out.println("新密码不能与旧密码相同");
            returnLevel();
        }
        if (!newPwd.equals(rePwd)) {
            System.out.println("两次密码不一致！");
            returnLevel();
        }
        String id = getInfo().get("bussiness_id").toString();
        int r = bussinessDao.updatePwd(newPwd, oldPwd, id);
        if (r > 0) {
            System.out.println("修改密码成功！");
        } else {
            System.out.println("修改失败！");
        }
        returnLevel();
    }

    private void addFood() {
        /*添加食品*/
        System.out.println("请输入商品信息：");
        System.out.println("商品名称：");
        String name = Read.getIn().next();
        System.out.println("商品价格");
        String price = Read.getIn().next();
        System.out.println("商品介绍");
        String info = Read.getIn().next();
        String id = getInfo().get("bussiness_id").toString();
        int row = foodDao.addFood(name, price, info, id);
        if (row > 0) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
        returnLevel();
    }

    private void searchFoodMenu() {
        //查询菜单
        System.out.println("1.查询所有食物");
        System.out.println("2.按名称查询");
        int n = Read.getIn().nextInt();
        switch (n) {
            case 1:
                searchFoodAll();
                break;
            case 2:
                searchFoodByName();
                break;
            default:
                System.out.println("请选择正确的功能！");
                break;
        }
    }


    private void searchFoodAll() {
        //从登录成功后存放的用户信息中获取商家的id
        String id = getInfo().get("bussiness_id").toString();
        List<Map<String, Object>> list = foodDao.findAll(id);
        System.out.println("食品编号\t食品名称\t食品简介\t食品价格\t食品状态");
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Object foodId = map.get("food_id");
            Object foodName = map.get("food_name");
            Object foodExplain = map.get("food_explain");
            Object foodPrice = map.get("food_price");
            Object foodStatus = map.get("food_status");
            System.out.println(foodId + "\t" + foodName + "\t" + foodExplain + "\t" + foodPrice + "\t" + foodStatus);

        }
        returnLevel();
    }

    private void searchFoodByName() {
        System.out.println("请输入食品名称：");
        String name = Read.getIn().next();
        String id = getInfo().get("bussiness_id").toString();
        List<Map<String, Object>> list = foodDao.findByName(name, id);
        System.out.println("食品编号\t食品名称\t食品简介\t食品价格\t食品状态");
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Object foodId = map.get("food_id");
            Object foodName = map.get("food_name");
            Object foodExplain = map.get("food_explain");
            Object foodPrice = map.get("food_price");
            Object foodStatus = map.get("food_status");
            System.out.println(id + "\t" + foodName + "\t" + foodExplain + "\t" + foodPrice + "\t" + foodStatus);
        }
        returnLevel();
    }
}
